import { useMemo } from 'react';
import { FacilityDevice, HVACDevice } from '../types';

/**
 * 自定义hook：处理HVAC设备数据
 * @param facilities 所有设施设备数据
 * @returns 过滤并格式化后的HVAC设备数据
 */
export const useHvacDevices = (
  facilities: FacilityDevice[]
): HVACDevice[] => {
  const hvacDevices = useMemo(() => {
    return facilities
      .filter(facility => ['AC', 'Heater', 'Fan', 'Ventilation'].includes(facility.type))
      .map(facility => {
        // 类型断言确保我们可以安全访问HVACDevice特有的属性
        const device = facility as HVACDevice;
        return {
          ...device,
          // 确保status字段符合要求
          status: (device.status as 'online' | 'offline' | 'warning' | 'error') || 'offline',
          // 确保必需的字段都有值
          temperatureSetting: 'temperatureSetting' in device ? device.temperatureSetting : 0,
          fanSpeed: 'fanSpeed' in device ? device.fanSpeed : 0,
          energyConsumption: 'energyConsumption' in device ? device.energyConsumption : 0
        };
      });
  }, [facilities]);

  return hvacDevices;
};

export default useHvacDevices;